package com.xls.future.model;

import android.content.Context;
import android.text.TextUtils;

import com.xls.future.FutureApp;
import com.xls.future.entity.Photo;
import com.xls.future.utils.AppUtil;
import com.xls.future.utils.LogUtil;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/28.
 */

public class HomeModelImpl implements HomeModel {

    private static final String TAG = "HomeModelImpl";

    @Override
    public void loadPicsForBanner(Context context,Subscriber<List<Photo>> subscriber) {
        final BmobQuery<Photo> bmobQuery = new BmobQuery<Photo>();
        bmobQuery.addWhereEqualTo("orientation",2);
        boolean isCache = bmobQuery.hasCachedResult(Photo.class);
        boolean isNetwork = AppUtil.isNetworkAvalible(context);
        LogUtil.d(TAG,"loadPhotosByPage isCache = "+isCache+" | isNetwork="+isNetwork);

        if(isCache && !isNetwork){
            bmobQuery.setCachePolicy(BmobQuery.CachePolicy.CACHE_ONLY);	// 如果有缓存的话，则先从缓存中获取
        }else{
            bmobQuery.setCachePolicy(BmobQuery.CachePolicy.NETWORK_ELSE_CACHE);	// 如果没有缓存的话，则先从网络中取
        }
        bmobQuery.findObjectsObservable(Photo.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

    }

    @Override
    public void loadPhotosByPage(Context context,String name, Subscriber<List<Photo>> subscriber) {
        final BmobQuery<Photo> bmobQuery = new BmobQuery<Photo>();
        if(!TextUtils.isEmpty(name)){
            bmobQuery.addWhereEqualTo("name",name);
        }
        bmobQuery.addWhereEqualTo("orientation",1);
        bmobQuery.setLimit(30);
        boolean isCache = bmobQuery.hasCachedResult(Photo.class);
        boolean isNetwork = AppUtil.isNetworkAvalible(context);
        LogUtil.d(TAG,"loadPhotosByPage isCache = "+isCache+" | isNetwork="+isNetwork);

        if(isCache && !isNetwork){
            bmobQuery.setCachePolicy(BmobQuery.CachePolicy.CACHE_ONLY);	// 如果有缓存的话，则先从缓存中获取
        }else{
            bmobQuery.setCachePolicy(BmobQuery.CachePolicy.NETWORK_ELSE_CACHE);	// 如果没有缓存的话，则先从网络中取
        }

        bmobQuery.findObjectsObservable(Photo.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    @Override
    public void loadPhotosMore(Context context,String name,int skipCount,Subscriber<List<Photo>> subscriber) {
        final BmobQuery<Photo> bmobQuery = new BmobQuery<Photo>();
        if(!TextUtils.isEmpty(name)){
            bmobQuery.addWhereEqualTo("name",name);
        }
        bmobQuery.addWhereEqualTo("orientation",1);
        bmobQuery.setSkip(skipCount);
        bmobQuery.setLimit(30);
        boolean isCache = bmobQuery.hasCachedResult(Photo.class);
        boolean isNetwork = AppUtil.isNetworkAvalible(context);
        LogUtil.d(TAG,"loadPhotosMore isCache = "+isCache+" | isNetwork="+isNetwork);

        if(isCache && !isNetwork){
            bmobQuery.setCachePolicy(BmobQuery.CachePolicy.CACHE_ONLY);	// 如果有缓存的话，则先从缓存中获取
        }else{
            bmobQuery.setCachePolicy(BmobQuery.CachePolicy.NETWORK_ELSE_CACHE);	// 如果没有缓存的话，则先从网络中取
        }
        bmobQuery.findObjectsObservable(Photo.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
}
